import torch
import torch.nn as nn
import torch.nn.functional as F


class LeNet(nn.Module):
    def __init__(self):
        super().__init__()
        # in (3,64,64)
        self.conv1 = nn.Conv2d(3, 16, 5)  # out (16,60,60)
        self.pool1 = nn.MaxPool2d(2, 2)  # (16,30,30)
        self.conv2 = nn.Conv2d(16, 32, 5)  # out (32,26,26)
        self.pool2 = nn.MaxPool2d(2, 2)  # out (32,13,13)

        self.fc1 = nn.Linear(32 * 13 * 13, 160)
        self.fc2 = nn.Linear(160, 84)
        self.fc3 = nn.Linear(84, 2)

    def forward(self, x):
        x = F.relu(self.conv1(x))  # 第一层卷积
        x = self.pool1(x)  # 池化下采样
        x = F.relu(self.conv2(x))  # 第二层卷积
        x = self.pool2(x)  # 池化下采样
        # x = x.view(-1, 32*5*5)
        x = torch.flatten(x, 1)  # torch的新展平方法 从dim = 1开始展平
        x = F.relu(self.fc1(x))  # 全连接1
        x = F.relu(self.fc2(x))  # 全连接2
        x = self.fc3(x)  # 全连接3 输出 输出不需要softmax 交叉熵自带了softmax
        return x
